/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ems.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.ems.entity.EmsStudent;
import com.thinkgem.jeesite.modules.ems.entity.EmsUser;
import com.thinkgem.jeesite.modules.ems.service.EmsUserService;
import com.thinkgem.jeesite.modules.sys.service.SystemService;

/**
 * 报名人Controller
 * @author zad
 * @version 2018-01-25
 */
@Controller
@RequestMapping(value = "${adminPath}/ems/emsUser")
public class EmsUserController extends BaseController {

	@Autowired
	private EmsUserService emsUserService;
	@Autowired
	private SystemService systemService;
	
	@ModelAttribute
	public EmsUser get(@RequestParam(required=false) String id) {
		EmsUser entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = emsUserService.get(id);
		}
		if (entity == null){
			entity = new EmsUser();
		}
		return entity;
	}
	
	@RequiresPermissions("ems:emsUser:view")
	@RequestMapping(value = {"list", ""})
	public String list(EmsUser emsUser, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EmsUser> page = emsUserService.findPage(new Page<EmsUser>(request, response), emsUser); 
		model.addAttribute("page", page);
		return "modules/ems/emsUserList";
	}

	@RequiresPermissions("ems:emsUser:view")
	@RequestMapping(value = "form")
	public String form(EmsUser emsUser, Model model) {
		model.addAttribute("emsUser", emsUser);
		return "modules/ems/emsUserForm";
	}
	
	@RequiresPermissions("ems:emsUser:view")
	@RequestMapping(value = "changePwd")
	public String changePwd(EmsUser emsUser, Model model) {
		model.addAttribute("emsUser", emsUser);
		return "modules/ems/emsUserChangePwd";
	}

	@RequiresPermissions("ems:emsUser:edit")
	@RequestMapping(value = "save")
	public String save(EmsUser emsUser, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, emsUser)){
			return form(emsUser, model);
		}
		
		// add by zad 密码加密
		if(null != emsUser)
		{
			emsUser.setPassword(SystemService.entryptPassword(emsUser.getPassword()));
		}
		
		emsUserService.save(emsUser);
		addMessage(redirectAttributes, "保存报名人成功");
		return "redirect:"+Global.getAdminPath()+"/ems/emsUser/?repage";
	}
	
	@RequiresPermissions("ems:emsUser:edit")
	@RequestMapping(value = "delete")
	public String delete(EmsUser emsUser, RedirectAttributes redirectAttributes) {
		emsUserService.delete(emsUser);
		addMessage(redirectAttributes, "删除报名人成功");
		return "redirect:"+Global.getAdminPath()+"/ems/emsUser/?repage";
	}

	// add by zad 2018-01-31 登录名和手机号码重复校验
	@RequiresPermissions("ems:emsUser:view")
	@RequestMapping(value = {"checkExists"})
	public String checkExists(EmsUser emsUser, HttpServletRequest request, HttpServletResponse response, Model model) {
		
		// 默认登录名和手机号码都不重复
		Map<String,String> rs = new HashMap<String,String>();
		rs.put("loginName", "no");
		rs.put("mobile", "no");
		
		EmsUser param = new EmsUser();
		
		List<EmsUser> emsUsers = null;
		// 有登录名则校验登录名
		if(null != emsUser && StringUtils.isNotBlank(emsUser.getLoginName()))
		{
			param.setLoginName(emsUser.getLoginName());
			emsUsers = this.emsUserService.checkExists(param);
			if(null != emsUsers && emsUsers.size() > 0)
			{
				rs.put("loginName", "yes");
			}
			else
			{
				rs.put("loginName", "no");
			}
		}
		
		// 有手机号码则校验手机号码
		if(null != emsUser && StringUtils.isNotBlank(emsUser.getMobile()))
		{
			param = new EmsUser();
			param.setMobile(emsUser.getMobile());
			emsUsers = this.emsUserService.checkExists(param);
			if(null != emsUsers && emsUsers.size() > 0)
			{
				rs.put("mobile", "yes");
			}
			else
			{
				rs.put("mobile", "no");
			}
		}
		
		String jsonStr = JsonMapper.toJsonString(rs);
		try {
			response.getWriter().print(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}